package com.lechuang.ssgou.presenter.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.design.widget.TabLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.MemoryCategory;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import com.lechuang.ssgou.R;
import com.lechuang.ssgou.model.bean.HomeBannerBean;
import com.lechuang.ssgou.model.bean.HomeGunDongTextBean;
import com.lechuang.ssgou.model.bean.HomeKindBean;
import com.lechuang.ssgou.model.bean.HomeLastProgramBean;
import com.lechuang.ssgou.model.bean.HomeProgramBean;
import com.lechuang.ssgou.model.bean.HomeSearchResultBean;
import com.lechuang.ssgou.presenter.CommonAdapter;
import com.lechuang.ssgou.view.defineView.AutoTextView;
import com.lechuang.ssgou.view.defineView.MGridView;
import com.lechuang.ssgou.view.defineView.SpannelTextView;
import com.lechuang.ssgou.view.defineView.SquareImageView;
import com.lechuang.ssgou.view.defineView.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import se.emilsjolander.stickylistheaders.StickyListHeadersAdapter;

/**
 * 作者：li on 2017/11/12 09:51
 * 邮箱：961567115@qq.com
 * 修改备注:
 */
public class MyHomeAdapter extends BaseAdapter implements StickyListHeadersAdapter {
    List<HomeSearchResultBean.ProductListBean> listProduct;
    private int images[] = {R.drawable.icon_gaunggang, R.drawable.icon_9, R.drawable.icon_1,
            R.drawable.icon_youxuan, R.drawable.icon_live};
    private String str[] = {"官方推荐", "9.9包邮", "1元试用", "品牌优选", "直播间"};

    public Context context;
    private  HomeSearchResultBean.ProductListBean productListBean;
    private View heand1;



    public MyHomeAdapter(Context context) {
        this.context = context;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        if (getHeaderId(position) == 0) {
                Heander1 heander1;
                if (convertView == null) {
                    heander1 = new Heander1();
                    convertView = View.inflate(context, R.layout.head1, null);
                    heander1.rvBanner = (RollPagerView) convertView.findViewById(R.id.rv_banner);
                    heander1.tvAutoText = (AutoTextView) convertView.findViewById(R.id.tv_auto_text);
                    heander1.ivProgram1 = (ImageView) convertView.findViewById(R.id.iv_program1);
                    heander1.ivProgram2 = (ImageView) convertView.findViewById(R.id.iv_program2);
                    heander1.ivProgram3 = (ImageView) convertView.findViewById(R.id.iv_program3);
                    heander1.ivProgram4 = (ImageView) convertView.findViewById(R.id.iv_program4);
                    heander1.ivJump = convertView.findViewById(R.id.iv_jump);
                    heander1.lastRollViewPager = (ImageView) convertView.findViewById(R.id.lastRollViewPager);
                    heander1.gvKind = (MGridView) convertView.findViewById(R.id.gv_kind);
                    convertView.setTag(heander1);
                } else {
                    heander1 = (Heander1) convertView.getTag();
                }
                heander1.setHeadData1();
                return convertView;
        } else {
            Heander2 heander2;
            if (getHeaderId(position) == 1) {
                heander2 = new Heander2();
                convertView = View.inflate(context, R.layout.head2, null);
                heander2.tlTab = (TabLayout) convertView.findViewById(R.id.tl_tab);
                heander2.ivItem = (ImageView) convertView.findViewById(R.id.iv_item);
                convertView.setTag(heander2);
            } else {
                heander2 = (Heander2) convertView.getTag();
            }
            heander2.setHeadData2();
            return convertView;
        }

    }

    @Override
    public long getHeaderId(int position) {
        if(position==0){
            return 0;
        }
        return 1;
    }
    @Override
    public int getCount() {
        return listProduct == null ? 0 : listProduct.size();
    }

    @Override
    public Object getItem(int position) {
        return listProduct.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ItemViewHolder item;
        if (convertView == null||productListBean.itemType == 1) {
            convertView = View.inflate(context, R.layout.item_last_program, null);
            item = new ItemViewHolder(convertView);
            convertView.setTag(item);
        } else {
            item = (ItemViewHolder) convertView.getTag();
        }
        if(listProduct!=null){
            item.setItemData(listProduct.get(position));
        }
        return convertView;
    }

    List<String> imgList = new ArrayList<>(); //轮播图
    private ArrayList<String> text = new ArrayList<>(); //
    List<String> imgProgrem = new ArrayList<>();

    //首页轮播图数据
    private List<HomeBannerBean.IndexBannerList> bannerList;
    //轮播文字
    private List<HomeGunDongTextBean.IndexMsgListBean> textlist;
    //四张图片数据
    private List<HomeProgramBean.ListBean> listProgrm;
    //最底部的图片
    private List<HomeLastProgramBean.programaBean.ListBean> lastImage;
//头一
    private class Heander1 {
        MGridView gvKind;
        RollPagerView rvBanner;
        AutoTextView tvAutoText;
        ImageView ivProgram1;
        ImageView ivProgram2;
        ImageView ivProgram3;
        ImageView ivProgram4;
        View ivJump;
        ImageView lastRollViewPager;

        public void setHeadData1() {
           /* if(bannerList!=null) {
                if(imgList!=null){
                    imgList.clear();
                }
                for (int i = 0; i < bannerList.size(); i++) {
                    imgList.add(bannerList.get(i).img);
                }

                //设置播放时间间隔
                rvBanner.setPlayDelay(3000);
                //设置透明度
                rvBanner.setAnimationDurtion(500);
                //设置适配器
                rvBanner.setAdapter(new BannerAdapter(context, imgList));
                //设置指示器（顺序依次）
                //自定义指示器图片
                //设置圆点指示器颜色
                //设置文字指示器
                //隐藏指示器
                //mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
                rvBanner.setHintView(new ColorPointHintView(context, Color.YELLOW, Color.WHITE));
            }*/

            List<Map<String, Object>> list = new ArrayList<>();
            for (int i = 0; i < images.length; i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("image", images[i]);
                map.put("name", str[i]);
                list.add(map);
            }
            //分类条目
            gvKind.setAdapter(new CommonAdapter<Map<String, Object>>(context, list, R.layout.home_kinds_item1) {
                @Override
                public void setData(com.lechuang.ssgou.view.defineView.ViewHolder viewHolder, Object item) {
                    viewHolder.getView(R.id.iv_kinds_img).setVisibility(View.GONE);
                    viewHolder.getView(R.id.iv_own).setVisibility(View.VISIBLE);
                    HashMap<String, Object> map = (HashMap<String, Object>) item;
                    String name = (String) map.get("name");
                    int img = (int) map.get("image");
                    viewHolder.setText(R.id.tv_kinds_name, name);
                    viewHolder.setImageResource(R.id.iv_own, img);

                }
            });

            if(textlist!=null){
                for (int i = 0; i < textlist.size(); i++) {
                    text.add(textlist.get(i).name);
                }

                //滚动文字
                //自定义的滚动textview
                tvAutoText.setTextAuto(text);
                //设置时间
                tvAutoText.setGap(3000);
            }

            if(listProgrm!=null){
                //四张图片
                for (int i = 0; i < listProgrm.size(); i++) {
                    if (listProgrm.get(i) != null)
                        imgList.add(listProgrm.get(i).img);
                }
                //栏目1
                if ( imgList.get(0) != null)
                    Glide.with(context).load(imgList.get(0)).into(ivProgram1);
                //栏目2
                if ( imgList.get(1) != null)
                    Glide.with(context).load(imgList.get(1)).into(ivProgram2);
                //栏目3
                if ( imgList.get(2) != null)
                    Glide.with(context).load(imgList.get(2)).into(ivProgram3);
                //栏目4
                if ( imgList.get(3) != null)
                    Glide.with(context).load(imgList.get(3)).into(ivProgram4);

            }
          if(lastImage!=null) {
              //广告图
              Glide.with(context).load(lastImage.get(0).img).into(lastRollViewPager);
          }
        }
    }
    //下面的标题
    List<HomeKindBean.ListBean> head2list;
    //tablayoute标题
    private List<HomeKindBean.ListBean> topTabList=new ArrayList<>();
    //头二
    private class Heander2 {
        TabLayout tlTab;

        ImageView ivItem;

        public void setHeadData2() {
            if(head2list!=null){
                //取前10类
                if (head2list.size() > 10) {
                    topTabList.clear();
                    head2list = head2list.subList(0, 10);
                }
                HomeKindBean.ListBean topTab = new HomeKindBean.ListBean();
                topTab.rootId = -1;
                topTab.rootName = "全部";
                topTabList.add(topTab);
                List<HomeKindBean.ListBean> tabList = head2list;
                tabList.subList(0, 10);
                if (tabList != null) {
                    topTabList.addAll(tabList);
                }

                for (int i = 0; i < topTabList.size(); i++) {
                    TabLayout.Tab tab = tlTab.newTab();
                    tab.setText(topTabList.get(i).rootName).setTag(i);
                    tlTab.addTab(tab);
                }
                //设置tablout 滑动模式
                tlTab.setTabMode(TabLayout.MODE_SCROLLABLE);
            }
        }
    }



//条目
    public class ItemViewHolder {
        @BindView(R.id.iv_img)
        SquareImageView ivImg;
        @BindView(R.id.spannelTextView)
        SpannelTextView spannelTextView;
        @BindView(R.id.tv_oldprice)
        TextView tvOldprice;
        @BindView(R.id.tv_get)
        TextView tvGet;
        @BindView(R.id.tv_nowprice)
        TextView tvNowprice;
        @BindView(R.id.tv_xiaoliang)
        TextView tvXiaoliang;
        @BindView(R.id.tv_quanMoney)
        TextView tvQuanMoney;
        @BindView(R.id.ll_layout)
        LinearLayout llLayout;

        public ItemViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

        public void setItemData(HomeSearchResultBean.ProductListBean bean) {
            //商品图
            Glide.with(context).load(bean.imgs).into(ivImg);
//        //动态调整滑动时的内存占用
            Glide.get(context).setMemoryCategory(MemoryCategory.LOW);
            //领券减金额
            tvQuanMoney.setText("领券减" + bean.couponMoney);
            //原价
            tvOldprice.setText(bean.price + "");
            //中划线
            tvOldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            //券后价
            tvNowprice.setText("¥" + bean.preferentialPrice);
            //赚
            tvGet.setText("赚¥" + bean.couponMoney);

            //商品名称
            spannelTextView.setShopType(Integer.parseInt(bean.shopType));
            spannelTextView.setDrawText(bean.name);
            //销量
            tvXiaoliang.setText("已抢" + bean.nowNumber + "件");
        }
    }


    public void setListProgrm(List<HomeProgramBean.ListBean> listProgrm) {
        if(this.listProgrm!=null){
            this.listProgrm.clear();
        }
        this.listProgrm = listProgrm;
        notifyDataSetChanged();
    }

    public void setHead2list(List<HomeKindBean.ListBean> head2list) {
        if(this.head2list!=null){
            this.head2list.clear();
        }
        this.head2list = head2list;
        notifyDataSetChanged();
    }

    public void setListProduct(List<HomeSearchResultBean.ProductListBean> listProduct) {
        if(this.listProduct!=null){
            this.listProduct.clear();
        }
        this.listProduct = listProduct;
        productListBean = new HomeSearchResultBean.ProductListBean();
        productListBean.itemType=1;
        this.listProduct.add(0, productListBean);
        notifyDataSetChanged();
    }

    public void setBannerList(List<HomeBannerBean.IndexBannerList> bannerList) {
        this.bannerList = bannerList;
        notifyDataSetChanged();
    }

    public void setTextlist(List<HomeGunDongTextBean.IndexMsgListBean> textlist) {
        this.textlist = textlist;
        notifyDataSetChanged();
    }
    public void setLastImage(List<HomeLastProgramBean.programaBean.ListBean> lastImage) {
        this.lastImage= lastImage;
        notifyDataSetChanged();
    }
}
